package com.cloudsea.yunhaistores.controller;
import com.cloudsea.yunhaistores.service.StoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.OutputStream;
@Tag(name = "门店管理", description = "管理商户的门店信息，如生成二维码等")
@RestController
@RequestMapping("/stores")
public class StoreController {
    @Autowired
    private StoreService storeService;

    @Operation(summary = "生成门店点餐二维码", description = "为指定门店生成一个用于用户扫码点餐的二维码图片")
    @GetMapping("/{storeId}/qrcode")
    public void getStoreQrCode(@PathVariable Long storeId, HttpServletResponse response) throws IOException {
        // 设置响应头，告诉浏览器这是一个PNG图片
        response.setContentType("image/png");

        // 获取响应的输出流
        OutputStream outputStream = response.getOutputStream();

        try {
            // 调用Service层，将生成的二维码图片直接写入响应流
            storeService.generateOrderQrCode(storeId, outputStream);
        } finally {
            // 确保输出流被关闭
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}
